package com.open.capacity.reimburse.controller;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.entity.SysFileinfo;
import com.open.capacity.reimburse.entity.SysNotice;
import com.open.capacity.reimburse.entity.SysNoticeRead;
import com.open.capacity.reimburse.service.SysNoticeService;
import com.open.capacity.utils.SysUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 公告管理
 */
@RestController
@RequestMapping("notices")
@Api(tags = "公告模块api")
public class SysNoticeController {

    private static Logger logger = LoggerFactory.getLogger(SysNoticeController.class);

    @Autowired
    private SysNoticeService noticeService;


    /**
     * 查询公告
     *
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('notice:get/notices')")
    @ApiOperation(value = "查询公告")
    @GetMapping("/list")
    public PageResult<SysNotice> pageNotices(@RequestParam Map<String, Object> params) {
        return noticeService.pageAll(params);
    }

    /**
     * top5
     */
    @RequestMapping("/top5")
    @PreAuthorize("hasAuthority('notice:get/notices')")
    public Result top5(@RequestParam Map<String, Object> params) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        params.put("createUserid", loginUser.getId());
        params.put("status", 2);
        List<SysNotice> noticeList = noticeService.findTop5(params);
        return Result.succeed(noticeList);
    }

    /**
     * 查询公告详情
     *
     * @param keyValue
     * @return
     */
    @PreAuthorize("hasAuthority('notice:get/notices')")
    @ApiOperation(value = "查询公告")
    @GetMapping("/findById")
    public Result<SysNotice> findById(String keyValue) {
        return Result.succeed(noticeService.findById(keyValue));
    }

    /**
     * 查询公告阅读者
     *
     * @return
     */
    @PreAuthorize("hasAuthority('notice:get/notices')")
    @ApiOperation(value = "查询公告阅读者")
    @GetMapping("/findReads")
    public PageResult<SysNoticeRead> findReads(@RequestParam Map<String, Object> params) {
        return noticeService.pageReads(params);
    }

    /**
     * 公告新增或者更新
     *
     * @param notice
     * @return
     */
    @PreAuthorize("hasAnyAuthority('notice:post/notices','notice:put/notices')")
    @PostMapping("/saveOrUpdate")
    @LogAnnotation(module = "公告", name = "保存")
    public Result saveOrUpdate(@RequestBody SysNotice notice, String keyValue) {
        return noticeService.saveOrUpdate(notice, keyValue);
    }

    /**
     * 公告发布
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('notice:post/notices','notice:put/notices')")
    @PostMapping("/publish")
    @LogAnnotation(module = "公告", name = "发布")
    public Result publish(String id) {
        noticeService.publish(id);
        return Result.succeed("发布成功");
    }

    /**
     * 删除公告
     * delete /notices/1
     *
     * @param id
     */
    @PreAuthorize("hasAuthority('notice:delete/notices/{id}')")
    @ApiOperation(value = "删除公告")
    @DeleteMapping("/delete/{id}")
    @LogAnnotation(module = "公告", name = "删除")
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            noticeService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            return Result.failed("操作失败");
        }
    }
}
